package ch01.text00.tx09.converter;

import ch01.text00.tx09.TypeConverter;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;


/**
 * 实现把查询数据转换为：java.time.LocalDate
 *
 * @author V
 * @date 2025-03-20
 */
public class LocalDateConverter implements TypeConverter {
    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalDate.class);
    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        return switch (value) {
            case LocalDate localDate -> localDate;
            case LocalDateTime localDateTime -> localDateTime.toLocalDate();
            case Date sqlDate -> sqlDate.toLocalDate();
            case Timestamp timestamp -> timestamp.toLocalDateTime().toLocalDate();
            default -> null;
        };
    }
}
